package com.tyl.xtk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tyl.xtk.beans.dto.UserPutDto;
import com.tyl.xtk.beans.po.Role;
import com.tyl.xtk.beans.po.User;
import com.tyl.xtk.beans.query.PageQuery;
import com.tyl.xtk.beans.vo.PageResult;
import com.tyl.xtk.beans.vo.Result;
import com.tyl.xtk.config.validation.groups.Insert;
import com.tyl.xtk.config.validation.groups.Update;
import com.tyl.xtk.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.groups.Default;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * create by tyl at 2025/8/27
 */

@RestController
@RequestMapping("user")
@Tag(name = "用户管理")
public class UserController {

    @Resource
    UserService userService;

    @GetMapping()
    @Operation(summary = "获取用户列表， 分页")
    public PageResult<User> page(PageQuery pageQuery) {
        IPage<User> userIPage = userService.selectPage(pageQuery.toPage());
        return PageResult.success(userIPage);
    }

    @GetMapping("{id}")
    public Result<User> one(@PathVariable @NotNull Long id) {
        User user = userService.selectById(id);
        if (user == null) {
            return Result.fail("用戶不存在");
        }
        return Result.success(user);
    }

    @GetMapping("{id}/role")
    public Result<List<Role>> roles(@PathVariable @NotNull Long id) {
        List<Role> roles = userService.selectRoleListByUserId(id);
        return Result.success(roles);
    }

    @PostMapping
    @Operation(summary = "新增用户")
    public Result<User> post(@RequestBody @Validated({Insert.class, Default.class}) UserPutDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        boolean insert = userService.insert(user);
        if (!insert) {
            return Result.fail("新增用户失败");
        }
        return Result.success(user);
    }


    @PutMapping
    @Operation(summary = "修改用户信息")
    public Result<String> put(@RequestBody @Validated({Update.class, Default.class}) UserPutDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        boolean insert = userService.update(user);
        if (!insert) {
            return Result.fail("修改用户信息失败");
        }
        return Result.success("");
    }
    
    @DeleteMapping("{id}")
    @Operation(summary = "删除用户")
    public Result<String> delete(@PathVariable @NotNull Long id) {
        // 检查用户是否存在
        User user = userService.selectById(id);
        if (user == null) {
            return Result.fail("用户不存在");
        }
        
        // 删除用户
        boolean remove = userService.removeById(id);
        if (!remove) {
            return Result.fail("删除用户失败");
        }
        return Result.success("删除用户成功");
    }

}
